/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.afollestad.materialdialogssample.slice;

import com.afollestad.commons.color.CircleView;
import com.afollestad.commons.color.ColorChooserDialog;
import com.afollestad.commons.folderselector.FileChooserDialog;
import com.afollestad.commons.folderselector.FolderChooserDialog;
import com.afollestad.commons.simplelist.MaterialSimpleListAdapter;
import com.afollestad.commons.simplelist.MaterialSimpleListItem;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.StackingBehavior;

import com.afollestad.materialdialogs.internal.MDButton;
import com.afollestad.materialdialogs.internal.ThemeSingleton;

import com.afollestad.materialdialogs.utils.ResUtil;
import com.afollestad.materialdialogs.utils.UiUtil;
import com.afollestad.materialdialogssample.ButtonItemAdapter;
import com.afollestad.materialdialogssample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

import java.io.File;
import java.io.IOException;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener, FolderChooserDialog.FolderCallback, FileChooserDialog.FileCallback, ColorChooserDialog.ColorCallback {

    private ToastDialog toastDialog;
    private Button btnBasicNoTitle, btnBasic, btnBasicLongContent, btnBasicIcon, btnBasicCheckPrompt, btnStacked, btnNeutral, btnCallbacks, btnBasicListNoTitle, btnBasicList, btnBasicListLong, btnBasicListLongItems, btnBasicListCheckboxPrompt, btnBasicListLongPress, btnSingleChoice, btnSingleChoiceLongItems, btnMultiChoice, btnMultiChoiceLimitSelections, btnMultiChoiceMinSelections, btnMultiChoiceLongItems, btnMultiChoiceDisabledItem, btnSimpleDialogGuidelineSpec, btnCustomAdapter, btnColorChooserPrimary, btnColorChooserAccent, btnColorChooserCustomColors, btnColorChooserCustomColorsNoSub, btnThemed, btnShowCancelDismiss, btnFileChooser, btnFolderChooser, btnCustomView, btnCustomViewDatePicker, btnInput, btnInputCustomInvalidation, btnInputCheckPrompt, btnProgress, btnProgressIndeterminate, btnProgressIndeterminateHorizontal;
    private TextField passwordInput;
    private MDButton positiveAction;
    private int index = 0;
    private Thread thread;
    // color chooser dialog
    private int primaryPreselect;
    // UTILITY METHODS
    private int accentPreselect;
    private static Context mContext;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_main, null, false);
        mContext = this;
        btnBasicNoTitle = ResUtil.findViewById(rootLayout, ResourceTable.Id_basicNoTitle);
        btnBasic = ResUtil.findViewById(rootLayout, ResourceTable.Id_basic);
        btnBasicLongContent = ResUtil.findViewById(rootLayout, ResourceTable.Id_basicLongContent);
        btnBasicIcon = ResUtil.findViewById(rootLayout, ResourceTable.Id_basicIcon);
        btnBasicCheckPrompt = ResUtil.findViewById(rootLayout, ResourceTable.Id_basicCheckPrompt);
        btnStacked = ResUtil.findViewById(rootLayout, ResourceTable.Id_stacked);
        btnNeutral = ResUtil.findViewById(rootLayout, ResourceTable.Id_neutral);
        btnCallbacks = ResUtil.findViewById(rootLayout, ResourceTable.Id_callbacks);
        btnBasicListNoTitle = ResUtil.findViewById(rootLayout, ResourceTable.Id_listNoTitle);
        btnBasicList = ResUtil.findViewById(rootLayout, ResourceTable.Id_list);
        btnBasicListLong = ResUtil.findViewById(rootLayout, ResourceTable.Id_longList);
        btnBasicListLongItems = ResUtil.findViewById(rootLayout, ResourceTable.Id_list_longItems);
        btnBasicListCheckboxPrompt = ResUtil.findViewById(rootLayout, ResourceTable.Id_list_checkPrompt);
        btnBasicListLongPress = ResUtil.findViewById(rootLayout, ResourceTable.Id_list_longPress);
        btnSingleChoice = ResUtil.findViewById(rootLayout, ResourceTable.Id_singleChoice);
        btnSingleChoiceLongItems = ResUtil.findViewById(rootLayout, ResourceTable.Id_singleChoice_longItems);
        btnMultiChoice = ResUtil.findViewById(rootLayout, ResourceTable.Id_multiChoice);
        btnMultiChoiceLimitSelections = ResUtil.findViewById(rootLayout, ResourceTable.Id_multiChoiceLimited);
        btnMultiChoiceMinSelections = ResUtil.findViewById(rootLayout, ResourceTable.Id_multiChoiceLimitedMin);
        btnMultiChoiceLongItems = ResUtil.findViewById(rootLayout, ResourceTable.Id_multiChoice_longItems);
        btnMultiChoiceDisabledItem = ResUtil.findViewById(rootLayout, ResourceTable.Id_multiChoice_disabledItems);
        btnSimpleDialogGuidelineSpec = ResUtil.findViewById(rootLayout, ResourceTable.Id_simpleList);
        btnCustomAdapter = ResUtil.findViewById(rootLayout, ResourceTable.Id_customListItems);
        btnColorChooserPrimary = ResUtil.findViewById(rootLayout, ResourceTable.Id_colorChooser_primary);
        btnColorChooserAccent = ResUtil.findViewById(rootLayout, ResourceTable.Id_colorChooser_accent);
        btnColorChooserCustomColors = ResUtil.findViewById(rootLayout, ResourceTable.Id_colorChooser_customColors);
        btnColorChooserCustomColorsNoSub = ResUtil.findViewById(rootLayout, ResourceTable.Id_colorChooser_customColorsNoSub);
        btnThemed = ResUtil.findViewById(rootLayout, ResourceTable.Id_themed);
        btnShowCancelDismiss = ResUtil.findViewById(rootLayout, ResourceTable.Id_showCancelDismiss);
        btnFileChooser = ResUtil.findViewById(rootLayout, ResourceTable.Id_file_chooser);
        btnFolderChooser = ResUtil.findViewById(rootLayout, ResourceTable.Id_folder_chooser);
        btnCustomView = ResUtil.findViewById(rootLayout, ResourceTable.Id_customView);
        btnCustomViewDatePicker = ResUtil.findViewById(rootLayout, ResourceTable.Id_customView_datePicker);
        btnInput = ResUtil.findViewById(rootLayout, ResourceTable.Id_input);
        btnInputCustomInvalidation = ResUtil.findViewById(rootLayout, ResourceTable.Id_input_custominvalidation);
        btnInputCheckPrompt = ResUtil.findViewById(rootLayout, ResourceTable.Id_input_checkPrompt);
        btnProgress = ResUtil.findViewById(rootLayout, ResourceTable.Id_progress1);
        btnProgressIndeterminate = ResUtil.findViewById(rootLayout, ResourceTable.Id_progress2);
        btnProgressIndeterminateHorizontal = ResUtil.findViewById(rootLayout, ResourceTable.Id_progress3);

        setShapeElement(new UiUtil(this));
        setClickListeners();
        setButtonTextCaps();
        setUIContent(rootLayout);

        primaryPreselect = ResUtil.getColor(this, ResourceTable.Color_primary);
        accentPreselect = ResUtil.getColor(this, ResourceTable.Color_accent);

        getWindow().setNavigationBarColor(primaryPreselect);
        getWindow().setStatusBarColor(primaryPreselect);
    }

    /*
     This method is to set the Button text capital.
     */
    private void setButtonTextCaps() {
        btnBasicNoTitle.setText(btnBasicNoTitle.getText().toUpperCase());
        btnBasic.setText(btnBasic.getText().toUpperCase());
        btnBasicLongContent.setText(btnBasicLongContent.getText().toUpperCase());
        btnBasicIcon.setText(btnBasicIcon.getText().toUpperCase());
        btnBasicCheckPrompt.setText(btnBasicCheckPrompt.getText().toUpperCase());
        btnStacked.setText(btnStacked.getText().toUpperCase());
        btnNeutral.setText(btnNeutral.getText().toUpperCase());
        btnCallbacks.setText(btnCallbacks.getText().toUpperCase());
        btnBasicListNoTitle.setText(btnBasicListNoTitle.getText().toUpperCase());
        btnBasicList.setText(btnBasicList.getText().toUpperCase());
        btnBasicListLong.setText(btnBasicListLong.getText().toUpperCase());
        btnBasicListLongItems.setText(btnBasicListLongItems.getText().toUpperCase());
        btnBasicListCheckboxPrompt.setText(btnBasicListCheckboxPrompt.getText().toUpperCase());
        btnBasicListLongPress.setText(btnBasicListLongPress.getText().toUpperCase());
        btnSingleChoice.setText(btnSingleChoice.getText().toUpperCase());
        btnSingleChoiceLongItems.setText(btnSingleChoiceLongItems.getText().toUpperCase());
        btnMultiChoice.setText(btnMultiChoice.getText().toUpperCase());
        btnMultiChoiceLimitSelections.setText(btnMultiChoiceLimitSelections.getText().toUpperCase());
        btnMultiChoiceMinSelections.setText(btnMultiChoiceMinSelections.getText().toUpperCase());
        btnMultiChoiceLongItems.setText(btnMultiChoiceLongItems.getText().toUpperCase());
        btnMultiChoiceDisabledItem.setText(btnMultiChoiceDisabledItem.getText().toUpperCase());
        btnSimpleDialogGuidelineSpec.setText(btnSimpleDialogGuidelineSpec.getText().toUpperCase());
        btnCustomAdapter.setText(btnCustomAdapter.getText().toUpperCase());
        btnCustomView.setText(btnCustomView.getText().toUpperCase());
        btnCustomViewDatePicker.setText(btnCustomViewDatePicker.getText().toUpperCase());
        btnColorChooserPrimary.setText(btnColorChooserPrimary.getText().toUpperCase());
        btnColorChooserAccent.setText(btnColorChooserAccent.getText().toUpperCase());
        btnColorChooserCustomColors.setText(btnColorChooserCustomColors.getText().toUpperCase());
        btnColorChooserCustomColorsNoSub.setText(btnColorChooserCustomColorsNoSub.getText().toUpperCase());
        btnInput.setText(btnInput.getText().toUpperCase());
        btnInputCheckPrompt.setText(btnInputCheckPrompt.getText().toUpperCase());
        btnInputCustomInvalidation.setText(btnInputCustomInvalidation.getText().toUpperCase());
        btnThemed.setText(btnThemed.getText().toUpperCase());
        btnShowCancelDismiss.setText(btnShowCancelDismiss.getText().toUpperCase());
        btnFileChooser.setText(btnFileChooser.getText().toUpperCase());
        btnFolderChooser.setText(btnFolderChooser.getText().toUpperCase());
        btnProgress.setText(btnProgress.getText().toUpperCase());
        btnProgressIndeterminate.setText(btnProgressIndeterminate.getText().toUpperCase());
        btnProgressIndeterminateHorizontal.setText(btnProgressIndeterminateHorizontal.getText().toUpperCase());
    }

    /**
     * This method is used to set background shape for the buttons
     *
     * @param uiUtil get class to call methods
     */
    private void setShapeElement(UiUtil uiUtil) {
        btnBasicNoTitle.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasic.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicLongContent.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicIcon.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicCheckPrompt.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnStacked.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnNeutral.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnCallbacks.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicListNoTitle.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicList.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicListLong.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicListLongItems.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicListCheckboxPrompt.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnBasicListLongPress.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnSingleChoice.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnSingleChoiceLongItems.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnMultiChoice.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnMultiChoiceLimitSelections.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnMultiChoiceMinSelections.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnMultiChoiceLongItems.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnMultiChoiceDisabledItem.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnSimpleDialogGuidelineSpec.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnCustomAdapter.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnColorChooserPrimary.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnColorChooserAccent.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnColorChooserCustomColors.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnColorChooserCustomColorsNoSub.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnThemed.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnShowCancelDismiss.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnFileChooser.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnFolderChooser.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnCustomView.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnCustomViewDatePicker.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnInput.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnInputCustomInvalidation.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnInputCheckPrompt.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnProgress.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnProgressIndeterminate.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
        btnProgressIndeterminateHorizontal.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
    }

    /**
     * This method is used to set click listener for the buttons
     */
    private void setClickListeners() {
        btnBasicNoTitle.setClickedListener(this);
        btnBasic.setClickedListener(this);
        btnBasicLongContent.setClickedListener(this);
        btnBasicIcon.setClickedListener(this);
        btnBasicCheckPrompt.setClickedListener(this);
        btnStacked.setClickedListener(this);
        btnNeutral.setClickedListener(this);
        btnCallbacks.setClickedListener(this);
        btnBasicListNoTitle.setClickedListener(this);
        btnBasicList.setClickedListener(this);
        btnBasicListLong.setClickedListener(this);
        btnBasicListLongItems.setClickedListener(this);
        btnBasicListCheckboxPrompt.setClickedListener(this);
        btnBasicListLongPress.setClickedListener(this);
        btnSingleChoice.setClickedListener(this);
        btnSingleChoiceLongItems.setClickedListener(this);
        btnMultiChoice.setClickedListener(this);
        btnMultiChoiceLimitSelections.setClickedListener(this);
        btnMultiChoiceMinSelections.setClickedListener(this);
        btnMultiChoiceLongItems.setClickedListener(this);
        btnMultiChoiceDisabledItem.setClickedListener(this);
        btnSimpleDialogGuidelineSpec.setClickedListener(this);
        btnCustomAdapter.setClickedListener(this);
        btnColorChooserPrimary.setClickedListener(this);
        btnColorChooserAccent.setClickedListener(this);
        btnColorChooserCustomColors.setClickedListener(this);
        btnColorChooserCustomColorsNoSub.setClickedListener(this);
        btnThemed.setClickedListener(this);
        btnShowCancelDismiss.setClickedListener(this);
        btnFileChooser.setClickedListener(this);
        btnFolderChooser.setClickedListener(this);
        btnCustomView.setClickedListener(this);
        btnCustomViewDatePicker.setClickedListener(this);
        btnInput.setClickedListener(this);
        btnInputCustomInvalidation.setClickedListener(this);
        btnInputCheckPrompt.setClickedListener(this);
        btnProgress.setClickedListener(this);
        btnProgressIndeterminate.setClickedListener(this);
        btnProgressIndeterminateHorizontal.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_basicNoTitle:
                new MaterialDialog.Builder(this)
                        .content(ResourceTable.String_shareLocationPrompt)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .show();
                break;
            case ResourceTable.Id_basic:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_useGoogleLocationServicesPrompt)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .show();
                break;
            case ResourceTable.Id_basicLongContent:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_loremIpsum)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .checkBoxPrompt(ResourceTable.String_hello_world, true, new AbsButton.CheckedStateChangedListener() {
                            @Override
                            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                                if (isChecked) {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_015);
                                } else {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_000);
                                }
                            }
                        })
                        .show();
                break;
            case ResourceTable.Id_basicIcon:
                new MaterialDialog.Builder(this)
                        .icon(ResourceTable.Media_ic_launcher)
                        .limitIconToDefaultSize()
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_useGoogleLocationServicesPrompt)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .show();
                break;
            case ResourceTable.Id_basicCheckPrompt:
                new MaterialDialog.Builder(this)
                        .icon(ResourceTable.Media_ic_launcher)
                        .limitIconToDefaultSize()
                        .title(ResourceTable.String_permissionSample)
                        .positiveText(ResourceTable.String_allow)
                        .negativeText(ResourceTable.String_deny)
                        .onAny((dialog, which) -> showToast("Prompt checked? " + dialog.isPromptCheckBoxChecked()))
                        .checkBoxPrompt(ResourceTable.String_dont_ask_again, false, new AbsButton.CheckedStateChangedListener() {
                            @Override
                            public void onCheckedChanged(AbsButton absButton, boolean checked) {
                                if (checked) {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_015);
                                } else {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_000);
                                }
                            }
                        })
                        .show();
                break;
            case ResourceTable.Id_stacked:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_useGoogleLocationServicesPrompt)
                        .positiveText(ResourceTable.String_speedBoost)
                        .negativeText(ResourceTable.String_noThanks)
                        .btnStackedGravity(GravityEnum.END)
                        .stackingBehavior(StackingBehavior.ALWAYS)
                        .show();
                break;
            case ResourceTable.Id_neutral:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_useGoogleLocationServicesPrompt)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .neutralText(ResourceTable.String_more_info)
                        .show();
                break;
            case ResourceTable.Id_callbacks:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_useGoogleLocationServicesPrompt)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .neutralText(ResourceTable.String_more_info)
                        .onAny((dialog, which) -> showToast(which.name() + "!"))
                        .show();
                break;
            case ResourceTable.Id_listNoTitle:
                new MaterialDialog.Builder(this)
                        .items(ResUtil.getStringArray(this, ResourceTable.Strarray_social_networks))
                        .itemsCallback((dialog, view, which, text) -> showToast(which + ": " + text))
                        .show();
                break;
            case ResourceTable.Id_list:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsCallback((dialog, view, which, text) -> showToast(which + ": " + text))
                        .show();
                break;
            case ResourceTable.Id_longList:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_states)
                        .items(new String[]{"Alabama", "Alaska", "American Samoa", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Northern Marianas Islands", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Virgin Islands", "Washington", "West Virginia", "Wisconsin", "Wyoming"})
                        .itemsCallback((dialog, view, which, text) -> showToast(which + ": " + text))
                        .positiveText(ResourceTable.String_cancel)
                        .show();
                break;
            case ResourceTable.Id_list_longItems:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter is an online social networking service that enables users to send and read short 140-character messages called \"tweets\".",
                                "Google+ is an interest-based social network that is owned and operated by Google Inc. The service is Google\'s fourth foray into social networking.",
                                "Instagram is an online mobile photo-sharing, video-sharing and social networking service that enables its users to take pictures and videos, and share them on a variety of social networking platforms.",
                                "Facebook is an online social networking service headquartered in Menlo Park, California. Its website was launched on February 4, 2004, by Mark Zuckerberg with friends."})
                        .itemsCallback((dialog, view, which, text) -> showToast(which + ": " + text))
                        .positiveText(ResourceTable.String_cancel)
                        .show();
                break;
            case ResourceTable.Id_list_checkPrompt:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsCallback((dialog, view, which, text) -> showToast(which + ": " + text))
                        .checkBoxPrompt(ResourceTable.String_example_prompt, true, new AbsButton.CheckedStateChangedListener() {
                            @Override
                            public void onCheckedChanged(AbsButton absButton, boolean checked) {
                                if (checked) {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_015);
                                } else {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_000);
                                }
                            }
                        })
                        .positiveText(ResourceTable.String_cancel)
                        .show();
                break;
            case ResourceTable.Id_list_longPress:
                index = 0;
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsCallback((dialog, view, which, text) -> showToast(which + ": " + text))
                        .autoDismiss(false)
                        .itemsLongCallback((dialog, itemView, position, text) -> {
                            dialog.getItems().remove(position);
                            dialog.notifyItemsChanged();
                            return false;
                        })
                        .onNeutral((dialog, which) -> {
                            index++;
                            dialog.getItems().add("Item " + index);
                            dialog.notifyItemInserted(dialog.getItems().size() - 1);
                        })
                        .neutralText(ResourceTable.String_add_item)
                        .show();
                break;
            case ResourceTable.Id_singleChoice:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsDisabledIndices(1, 3)
                        .itemsCallbackSingleChoice(2, (dialog, view, which, text) -> {
                            showToast(which + ": " + text);
                            return true; // allow selection
                        })
                        .positiveText(ResourceTable.String_md_choose_label)
                        .show();
                break;
            case ResourceTable.Id_singleChoice_longItems:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter is an online social networking service that enables users to send and read short 140-character messages called \"tweets\".",
                                "Google+ is an interest-based social network that is owned and operated by Google Inc. The service is Google\'s fourth foray into social networking.",
                                "Instagram is an online mobile photo-sharing, video-sharing and social networking service that enables its users to take pictures and videos, and share them on a variety of social networking platforms.",
                                "Facebook is an online social networking service headquartered in Menlo Park, California. Its website was launched on February 4, 2004, by Mark Zuckerberg with friends."})
                        .itemsCallbackSingleChoice(2, (dialog, view, which, text) -> {
                            showToast(which + ": " + text);
                            return true; // allow selection
                        })
                        .positiveText(ResourceTable.String_md_choose_label)
                        .show();
                break;
            case ResourceTable.Id_multiChoice:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsCallbackMultiChoice(
                                new Integer[]{1, 3},
                                (dialog, which, text) -> {
                                    StringBuilder str = new StringBuilder();
                                    for (int i = 0; i < which.length; i++) {
                                        if (i > 0) {
                                            str.append('\n');
                                        }
                                        str.append(which[i]);
                                        str.append(": ");
                                        str.append(text[i]);
                                    }
                                    showToast(str.toString());
                                    return true; // allow selection
                                })
                        .onNeutral((dialog, which) -> dialog.clearSelectedIndices(true))
                        .onPositive((dialog, which) -> dialog.hide())
                        .alwaysCallMultiChoiceCallback()
                        .positiveText(ResourceTable.String_md_choose_label)
                        .autoDismiss(false)
                        .neutralText(ResourceTable.String_clear_selection)
                        .show();
                break;
            case ResourceTable.Id_multiChoiceLimited:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsCallbackMultiChoice(new Integer[]{1}, (dialog, which, text) -> {
                            // limit selection to 2, the new (un)selection is included in the which array
                            boolean allowSelectionChange = which.length <= 2;
                            if (!allowSelectionChange) {
                                showToast(ResUtil.getString(this, ResourceTable.String_selection_limit_reached));
                            }
                            return allowSelectionChange;
                        })
                        .positiveText(ResourceTable.String_dismiss)
                        .alwaysCallMultiChoiceCallback() // the callback will always be called, to check if (un)selection is still allowed
                        .show();
                break;
            case ResourceTable.Id_multiChoiceLimitedMin:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsCallbackMultiChoice(new Integer[]{1}, (dialog, which, text) -> {
                            // selection count must stay above 1, the new (un)selection is included in the which array
                            boolean allowSelectionChange = which.length >= 1;
                            if (!allowSelectionChange) {
                                showToast(ResUtil.getString(this, ResourceTable.String_selection_min_limit_reached));
                            }
                            return allowSelectionChange;
                        })
                        .positiveText(ResourceTable.String_dismiss)
                        .alwaysCallMultiChoiceCallback() // the callback will always be called, to check if (un)selection is still allowed
                        .show();
                break;
            case ResourceTable.Id_multiChoice_longItems:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter is an online social networking service that enables users to send and read short 140-character messages called \"tweets\".",
                                "Google+ is an interest-based social network that is owned and operated by Google Inc. The service is Google\'s fourth foray into social networking.",
                                "Instagram is an online mobile photo-sharing, video-sharing and social networking service that enables its users to take pictures and videos, and share them on a variety of social networking platforms.",
                                "Facebook is an online social networking service headquartered in Menlo Park, California. Its website was launched on February 4, 2004, by Mark Zuckerberg with friends."})
                        .itemsCallbackMultiChoice(new Integer[]{1, 3}, (dialog, which, text) -> {
                            StringBuilder str = new StringBuilder();
                            for (int i = 0; i < which.length; i++) {
                                if (i > 0) {
                                    str.append('\n');
                                }
                                str.append(which[i]);
                                str.append(": ");
                                str.append(text[i]);
                            }
                            showToast(str.toString());
                            return true; // allow selection
                        })
                        .positiveText(ResourceTable.String_md_choose_label)
                        .show();
                break;
            case ResourceTable.Id_multiChoice_disabledItems:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_socialNetworks)
                        .items(new String[]{"Twitter", "Google", "Instagram", "Facebook"})
                        .itemsCallbackMultiChoice(new Integer[]{0, 1, 2}, (dialog, which, text) -> {
                            StringBuilder str = new StringBuilder();
                            for (int i = 0; i < which.length; i++) {
                                if (i > 0) {
                                    str.append('\n');
                                }
                                str.append(which[i]);
                                str.append(": ");
                                str.append(text[i]);
                            }
                            showToast(str.toString());
                            return true; // allow selection
                        })
                        .onNeutral((dialog, which) -> dialog.clearSelectedIndices(true))
                        .alwaysCallMultiChoiceCallback()
                        .positiveText(ResourceTable.String_md_choose_label)
                        .autoDismiss(false)
                        .neutralText(ResourceTable.String_clear_selection)
                        .itemsDisabledIndices(0, 1)
                        .show();
                break;
            case ResourceTable.Id_simpleList:
                final MaterialSimpleListAdapter materialSimpleListAdapter =
                        new MaterialSimpleListAdapter(this,
                                (dialog, index1, item) -> showToast(item.getContent().toString()));
                materialSimpleListAdapter.add(
                        new MaterialSimpleListItem.Builder(this)
                                .content(ResUtil.getString(this, ResourceTable.String_user_email1))
                                .icon(ResourceTable.Media_ic_account_circle)
                                .backgroundColor(Color.WHITE.getValue())
                                .build());
                materialSimpleListAdapter.add(
                        new MaterialSimpleListItem.Builder(this)
                                .content(ResUtil.getString(this, ResourceTable.String_user_email2))
                                .icon(ResourceTable.Media_ic_account_circle)
                                .backgroundColor(Color.WHITE.getValue())
                                .build());
                materialSimpleListAdapter.add(
                        new MaterialSimpleListItem.Builder(this)
                                .content(ResourceTable.String_add_account)
                                .icon(ResourceTable.Media_ic_content_add)
                                .iconPadding(8)
                                .build());
                new MaterialDialog.Builder(this).title(ResourceTable.String_set_backup).baseItemProvider(materialSimpleListAdapter).show();
                break;
            case ResourceTable.Id_customListItems:
                final ButtonItemAdapter buttonItemAdapter = new ButtonItemAdapter(this, new String[]{"Twitter", "Google", "Instagram", "Facebook"});
                buttonItemAdapter.setCallbacks(
                        itemIndex -> showToast("Item clicked: " + itemIndex),
                        buttonIndex -> showToast("Button clicked: " + buttonIndex));
                new MaterialDialog.Builder(this).title(ResourceTable.String_socialNetworks).baseItemProvider(buttonItemAdapter).show();
                break;
            case ResourceTable.Id_customView:
                showCustomView();
                break;
            case ResourceTable.Id_customView_datePicker:
                showDatePickerView();
                break;
            case ResourceTable.Id_colorChooser_primary:
                new ColorChooserDialog.Builder(this, ResourceTable.String_color_palette)
                        .titleSub(ResourceTable.String_colors)
                        .preselect(primaryPreselect)
                        .show();
                break;
            case ResourceTable.Id_colorChooser_accent:
                new ColorChooserDialog.Builder(this, ResourceTable.String_color_palette)
                        .titleSub(ResourceTable.String_colors)
                        .accentMode(true)
                        .preselect(accentPreselect)
                        .show();
                break;
            case ResourceTable.Id_colorChooser_customColors:
                Color[] customColors = new Color[]{
                        new Color(0xFFF44336), new Color(0xFFE91E63), new Color(0xFF9C27B0),
                        new Color(0xFF673AB7), new Color(0xFF3F51B5), new Color(0xFF2196F3),
                };
                Color[][] subColors =
                        new Color[][]{
                                new Color[]{
                                        new Color(0xFFEF5350), new Color(0xFFF44336), new Color(0xFFE53935)
                                },
                                new Color[]{
                                        new Color(0xFFEC407A), new Color(0xFFE91E63), new Color(0xFFD81B60)
                                },
                                new Color[]{
                                        new Color(0xFFAB47BC), new Color(0xFF9C27B0), new Color(0xFF8E24AA)
                                },
                                new Color[]{
                                        new Color(0xFF7E57C2), new Color(0xFF673AB7), new Color(0xFF5E35B1)
                                },
                                new Color[]{
                                        new Color(0xFF5C6BC0), new Color(0xFF3F51B5), new Color(0xFF3949AB)
                                },
                                new Color[]{
                                        new Color(0xFF42A5F5), new Color(0xFF2196F3), new Color(0xFF1E88E5)
                                }
                        };
                new ColorChooserDialog.Builder(this, ResourceTable.String_color_palette)
                        .titleSub(ResourceTable.String_colors)
                        .preselect(primaryPreselect)
                        .customColors(customColors, subColors)
                        .show();
                break;
            case ResourceTable.Id_colorChooser_customColorsNoSub:
                Color[] customColorsNoSub = new Color[]{
                        new Color(0xFFF44336), new Color(0xFFE91E63), new Color(0xFF9C27B0),
                        new Color(0xFF673AB7), new Color(0xFF3F51B5), new Color(0xFF2196F3),
                };
                new ColorChooserDialog.Builder(this, ResourceTable.String_color_palette)
                        .titleSub(ResourceTable.String_colors)
                        .preselect(primaryPreselect)
                        .customColors(customColorsNoSub, null)
                        .show();
                break;
            case ResourceTable.Id_folder_chooser:
                checkWritePermission();
                break;
            case ResourceTable.Id_file_chooser:
                checkReadPermission();
                break;
            case ResourceTable.Id_input:
                new MaterialDialog.Builder(MainAbilitySlice.this)
                        .title(ResourceTable.String_input)
                        .content(ResourceTable.String_input_content)
                        .inputType(InputAttribute.PATTERN_TEXT)
                        .inputRange(2, 16)
                        .positiveText(ResourceTable.String_submit)
                        .input(
                                ResourceTable.String_input_hint,
                                ResourceTable.String_input_hint,
                                false,
                                (dialog, input) -> showToast("Hello, " + input.toString() + "!"))
                        .show();
                break;
            case ResourceTable.Id_input_custominvalidation:
                new MaterialDialog.Builder(MainAbilitySlice.this)
                        .title(ResourceTable.String_input)
                        .content(ResourceTable.String_input_content_custominvalidation)
                        .inputType(InputAttribute.PATTERN_TEXT)
                        .positiveText(ResourceTable.String_submit)
                        .alwaysCallInputCallback() // this forces the callback to be invoked with every input change
                        .input(
                                ResourceTable.String_input_hint,
                                0,
                                false,
                                (dialog, input) -> {
                                    if (input.toString().equalsIgnoreCase("hello")) {
                                        dialog.setContent("I told you not to type that!");
                                        dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
                                    } else {
                                        dialog.setContent(ResourceTable.String_input_content_custominvalidation);
                                        dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
                                    }
                                })
                        .show();
                break;
            case ResourceTable.Id_input_checkPrompt:
                new MaterialDialog.Builder(MainAbilitySlice.this)
                        .title(ResourceTable.String_input)
                        .content(ResourceTable.String_input_content)
                        .inputType(InputAttribute.PATTERN_TEXT)
                        .inputRange(2, 16)
                        .positiveText(ResourceTable.String_submit)
                        .input(
                                ResourceTable.String_input_hint,
                                ResourceTable.String_input_hint,
                                false,
                                (dialog, input) -> showToast("Hello, " + input.toString() + "!"))
                        .checkBoxPrompt(ResourceTable.String_example_prompt, true, new AbsButton.CheckedStateChangedListener() {
                            @Override
                            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                                if (isChecked) {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_015);
                                } else {
                                    setAbsButtonBackgroundElement(absButton, ResourceTable.Media_abc_btn_check_to_on_mtrl_000);
                                }
                            }
                        })
                        .show();
                break;
            case ResourceTable.Id_progress1:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_progress_dialog)
                        .content(ResourceTable.String_please_wait)
                        .contentGravity(GravityEnum.CENTER)
                        .progress(false, 150, true)
                        .showListener(
                                dialogBase -> {
                                    final MaterialDialog dialog = (MaterialDialog) dialogBase;
                                    startThread(
                                            () -> {
                                                while (dialog.getCurrentProgress() != dialog.getMaxProgress()
                                                        && !Thread.currentThread().isInterrupted()) {
                                                    try {
                                                        Thread.sleep(50);
                                                    } catch (InterruptedException e) {
                                                        break;
                                                    }
                                                    dialog.incrementProgress(1);
                                                }
                                                getUITaskDispatcher().asyncDispatch(
                                                        () -> {
                                                            thread = null;
                                                            dialog.setContent(ResUtil.getString(MainAbilitySlice.this, ResourceTable.String_md_done_label));
                                                        });
                                            });
                                }
                        )
                        .show();
                break;
            case ResourceTable.Id_progress2:
                showIndeterminateProgressDialog(false);
                break;
            case ResourceTable.Id_progress3:
                showIndeterminateProgressDialog(true);
                break;
            case ResourceTable.Id_themed:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_useGoogleLocationServicesPrompt)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .titleGravity(GravityEnum.CENTER)
                        .titleColor(ResourceTable.Color_material_red_400)
                        .contentColor(ResourceTable.Color_white)
                        .backgroundColor(ResourceTable.Color_material_blue_grey_800)
                        .dividerColor(ResourceTable.Color_accent)
                        .btnSelector(1, DialogAction.POSITIVE)
                        .positiveColor(ResourceTable.Color_white)
                        .negativeColor(ResourceTable.Color_textColorSecondaryInverse)
                        .show();
                break;
            case ResourceTable.Id_showCancelDismiss:
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_useGoogleLocationServices)
                        .content(ResourceTable.String_useGoogleLocationServicesPrompt)
                        .positiveText(ResourceTable.String_agree)
                        .negativeText(ResourceTable.String_disagree)
                        .neutralText(ResourceTable.String_more_info)
                        .showListener(dialog -> showToast("onShow"))
                        .cancelListener(dialog -> showToast("onCancel"))
                        .dismissListener(dialog -> showToast("onDismiss"))
                        .show();
                break;
        }
    }

    private void showIndeterminateProgressDialog(boolean horizontal) {
        new MaterialDialog.Builder(this)
                .title(ResourceTable.String_progress_dialog)
                .content(ResourceTable.String_please_wait)
                .progress(true, 0)
                .progressIndeterminateStyle(horizontal)
                .show();
    }

    private void showCustomView() {
        MaterialDialog dialog =
                new MaterialDialog.Builder(this)
                        .title(ResourceTable.String_googleWifi)
                        .customView(ResourceTable.Layout_dialog_customview, false)
                        .positiveText(ResourceTable.String_connect)
                        .negativeText(ResourceTable.String_cancel)
                        .onPositive(
                                (dialog1, which) -> showToast("Password: " + passwordInput.getText()))
                        .build();
        int widgetColor = (ThemeSingleton.get().widgetColor == 0) ? ResUtil.getColor(this, ResourceTable.Color_colorAccent) : ThemeSingleton.get().widgetColor;

        RgbColor rgbcolor = RgbColor.fromArgbInt(widgetColor);
        rgbcolor.setAlpha(80);
        Color transColor = new Color(rgbcolor.asArgbInt());

        positiveAction = dialog.getActionButton(DialogAction.POSITIVE);
        positiveAction.setTextColor(transColor);

        passwordInput = (TextField) dialog.getCustomView().findComponentById(ResourceTable.Id_password);
        passwordInput.setTextCursorVisible(true);

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(ShapeElement.LINE);
        shapeElement.setStroke(5, RgbColor.fromArgbInt(widgetColor));
        passwordInput.setBasement(shapeElement);
        passwordInput.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                boolean enable = s.trim().length() > 0;
                if (enable) {
                    positiveAction.setEnabled(true);
                    positiveAction.setTextColor(new Color(widgetColor));
                } else {
                    positiveAction.setEnabled(false);
                    positiveAction.setTextColor(transColor);
                }
            }
        });


        // Toggling the show password CheckBox will mask or unmask the password input EditText
        Checkbox checkbox = (Checkbox) dialog.getCustomView().findComponentById(ResourceTable.Id_showPassword);
        checkbox.setCheckedStateChangedListener((absButton, isChecked) -> {
            if (isChecked) {
                setAbsButtonBackgroundElement(checkbox, ResourceTable.Media_abc_btn_check_to_on_mtrl_015);
            } else {
                setAbsButtonBackgroundElement(checkbox, ResourceTable.Media_abc_btn_check_to_on_mtrl_000);
            }
            passwordInput.setTextInputType(
                    !isChecked ? InputAttribute.PATTERN_PASSWORD : InputAttribute.PATTERN_TEXT);
            passwordInput.setText(passwordInput.getText());
        });
        setAbsButtonBackgroundElement(checkbox, ResourceTable.Media_abc_btn_check_to_on_mtrl_000);
        positiveAction.setEnabled(false); // disabled by default
        dialog.getCustomView().setComponentSize(1600, 550);
        dialog.show();
    }

    /**
     * This method is used to show DatePicker with DMY format
     */
    private void showDatePickerView() {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .title(ResourceTable.String_date_picker)
                .customView(ResourceTable.Layout_dialog_datepicker, false)
                .positiveText(ResourceTable.String_ok)
                .negativeText(ResourceTable.String_cancel)
                .build();
        DatePicker datePicker = (DatePicker) dialog.getCustomView().findComponentById(ResourceTable.Id_datePicker);
        datePicker.setDateOrder(DatePicker.DateOrder.DMY);
        dialog.show();
    }

    /**
     * This method is used to set background element & background color of radiobutton & checkbox
     *
     * @param absButton Radiobutton/Checkbox
     * @param drawable  Resource drawable for checked/uncheked
     */
    private void setAbsButtonBackgroundElement(AbsButton absButton, int drawable) {
        absButton.setButtonElement(ResUtil.getPixelMapDrawable(this, drawable));
        absButton.setTextColorOn(new Color(ResUtil.getColor(this, ResourceTable.Color_material_gray)));
        absButton.setTextColorOff(new Color(ResUtil.getColor(this, ResourceTable.Color_material_gray)));
    }

    private void showToast(String message) {
        LayoutScatter layoutScatter = LayoutScatter.getInstance(this);
        Component component = layoutScatter.parse(ResourceTable.Layout_md_toast_layout, null, false);
        Text text = (Text) component.findComponentById(ResourceTable.Id_toast_text);
        text.setText(message);

        if (toastDialog != null) {
            toastDialog.cancel();
            toastDialog = null;
        }
        toastDialog = new ToastDialog(this);
        UiUtil uiUtil = new UiUtil(this);
        toastDialog.setContentCustomComponent(component);
        toastDialog.getComponent().setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_lighter_gray, 0.0f));
        toastDialog.setOffset(30, 100);
        toastDialog.show();
    }

    private void startThread(Runnable run) {
        if (thread != null) {
            thread.interrupt();
        }
        thread = new Thread(run);
        thread.start();
    }

    @Override
    public void onColorSelection(ColorChooserDialog dialog, int selectedColor) {
        if (dialog.isAccentMode()) {
            accentPreselect = selectedColor;
            ThemeSingleton.get().positiveColor = selectedColor;
            ThemeSingleton.get().negativeColor = selectedColor;
            ThemeSingleton.get().neutralColor = selectedColor;
            ThemeSingleton.get().widgetColor = selectedColor;
        } else {
            primaryPreselect = selectedColor;
            getWindow().setStatusBarColor(CircleView.shiftColorDown(selectedColor));
            getWindow().setNavigationBarColor(selectedColor);
        }


    }

    @Override
    public void onColorChooserDismissed(ColorChooserDialog dialog) {
        showToast("Color chooser dismissed!");
    }

    @Override
    public void onFileSelection(FileChooserDialog dialog, File file) {
        try {
            showToast(file.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFileChooserDismissed(FileChooserDialog dialog) {
        showToast("File chooser dismissed!");
    }

    @Override
    public void onFolderSelection(FolderChooserDialog dialog, File folder) {
        try {
            showToast(folder.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFolderChooserDismissed(FolderChooserDialog dialog) {
        showToast("Folder chooser dismissed!");
    }

    private void checkWritePermission() {
        if (verifySelfPermission(SystemPermission.WRITE_USER_STORAGE) == IBundleManager.PERMISSION_GRANTED) {
            showFolderChooserDialog();
        } else {
            requestPermissionsFromUser(new String[]{SystemPermission.WRITE_USER_STORAGE}, 0);
        }
    }

    private void checkReadPermission() {
        if (verifySelfPermission(SystemPermission.READ_USER_STORAGE) == IBundleManager.PERMISSION_GRANTED) {
            showFileChooserDialog();
        } else {
            requestPermissionsFromUser(new String[]{SystemPermission.READ_USER_STORAGE}, 0);
        }
    }

    /**
     * This method is called after permissions are granted/denied
     *
     * @param requestCode  To generate a request
     * @param permissions  List of permissions to be taken from user
     * @param grantResults Permisison result get by the user
     */
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        if (permissions == null || permissions.length == 0 || grantResults == null || grantResults.length == 0) {
            return;
        }
        if (requestCode == 0) {
            if (grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                if (permissions[0].equals(SystemPermission.READ_USER_STORAGE)) {
                    showFileChooserDialog();
                } else {
                    showFolderChooserDialog();
                }
            } else {
                showToast(ResUtil.getString(this, ResourceTable.String_permission_not_granted));
            }
        }
    }

    private void showFolderChooserDialog() {
        new FolderChooserDialog.Builder(mContext)
                .chooseButton(ResourceTable.String_md_choose_label)
                .allowNewFolder(true, 0)
                .show();
    }

    private void showFileChooserDialog() {
        new FileChooserDialog.Builder(mContext).show();
    }
}